package com.xl.qa.controller;

import com.xl.basic.annotation.RoleIgnored;
import com.xl.basic.annotation.RoleValidated;
import com.xl.basic.entity.Result;
import com.xl.basic.entity.ResultEnums;
import com.xl.basic.entity.SPage;
import com.xl.basic.entity.UserInfo;
import com.xl.basic.utils.LoginUtils;
import com.xl.dao.domain.XlQuestion;
import com.xl.qa.enmus.QuestionTypeEnums;
import com.xl.qa.entity.param.AddQuestionParam;
import com.xl.qa.entity.param.QueryQuestionParam;
import com.xl.qa.entity.param.UpdQuestionParam;
import com.xl.qa.entity.result.QuestionDetailsResult;
import com.xl.qa.service.QuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 钟勇
 * Create in 2024/1/17 10:11
 */
@Tag(name = "问卷管理")
@RestController
@RequestMapping("/api/Question")
@RequiredArgsConstructor
@RoleValidated
public class QuestionController {

    private final QuestionService questionService;

    @Operation(summary = "新增问卷")
    @PostMapping(value = "/addQuestion")
    public Result<Integer> addQuestion(@Validated AddQuestionParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return questionService.addQuestion(param, userInfoRes.getResult());
    }

    @Operation(summary = "编辑问卷")
    @PostMapping(value = "/updQuestion")
    public Result<Object> updQuestion(@Validated UpdQuestionParam param) {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        return questionService.updQuestion(param, userInfoRes.getResult());
    }

    @Operation(summary = "获取问卷明细")
    @PostMapping(value = "/getQuestionDetails")
    @RoleIgnored
    public Result<QuestionDetailsResult> getQuestionDetails(@NotNull(message = "问卷ID不能为空") Integer questionId) {
        return questionService.getQuestionDetails(questionId);
    }

    @Operation(summary = "查询问卷列表")
    @PostMapping(value = "/queryQuestionList")
    @RoleIgnored
    public Result<List<XlQuestion>> queryQuestionList(@Validated QueryQuestionParam param) {
        return questionService.queryQuestionList(param);
    }

    @Operation(summary = "查询问卷分页")
    @PostMapping(value = "/queryQuestionPage")
    @RoleIgnored
    public Result<List<XlQuestion>> queryQuestionPage(@Validated QueryQuestionParam param, SPage sPage) {
        return questionService.queryQuestionPage(param, sPage);
    }

    @Operation(summary = "查询问卷类型字典")
    @PostMapping(value = "/queryQuestionTypeList")
    @RoleIgnored
    public Result<List<QuestionTypeEnums.QuestionTypeVo>> queryQuestionTypeList() {
        return ResultEnums.Success.getListResult(QuestionTypeEnums.queryQuestionTypeList());
    }
}
